﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmListofuser : Form
    {
        DatabaseConnection dbconn = new DatabaseConnection();
        public frmListofuser()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmListofuser_Load);
            dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
            txtSearchUser.TextChanged += new EventHandler(txtSearchUser_TextChanged);
            dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_RowHeaderMouseClick);
        }
        void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView1.SelectedRows[0];
            this.Hide();
            frmUpdateUser frmUp = new frmUpdateUser();
            frmUp.Show();
            this.Hide();
            frmUp.txtuserID.Text = dr.Cells[0].Value.ToString();
            frmUp.txtUsername.Text = dr.Cells[0].Value.ToString();
            frmUp.cmbUserType.Text = dr.Cells[4].Value.ToString();
            frmUp.txtName.Text = dr.Cells[1].Value.ToString();
            frmUp.txtContact_no.Text = dr.Cells[2].Value.ToString();
            frmUp.txtEmail_Address.Text = dr.Cells[3].Value.ToString();
        }
        void txtSearchUser_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchUser.Text != "Search User")
            {
                dbconn.dbconnection();
                dbconn.loadData("SELECT distinct Username,Name,ContactNo,Email,Usertype,JoiningDate FROM tbluserinfo WHERE Username LIKE '%" + txtSearchUser.Text + "%'");
                dataGridView1.DataSource = dbconn.tbl;
                dbconn.DBClose();
            }
            else
            {
                GetData();
            }
        }

        void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            string strRowNumber = (e.RowIndex + 1).ToString();
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
            if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
            {
                dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
            }
            Brush b = SystemBrushes.ControlText;
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }
        void GetData()
        {
            dbconn.dbconnection();
            dbconn.loadData("Select distinct Username,Name,ContactNo,Email,Usertype,JoiningDate from tbluserinfo");
            dataGridView1.DataSource = dbconn.tbl;
            dbconn.DBClose();
        
     
        }
        void frmListofuser_Load(object sender, EventArgs e)
        {
            GetData();
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            
        }

        
    }
}
